package com.example.cenoo.itravel;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView mBottomNavigationView;
    private FrameLayout mContentContainer;
    private BaseFragment mQAFragment;
    private BaseFragment mStrategyFragment;
    private BaseFragment mPersonalFragment;
    private BaseFragment mTeamFragment;
    private BaseFragment currentFragment;

    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        disableShiftMode(mBottomNavigationView);
        mContentContainer = (FrameLayout) findViewById(R.id.content_container);

        mStrategyFragment = BaseFragment.newInstance(R.layout.frament_strategy);
        mFragmentManager = getSupportFragmentManager();
        mFragmentManager.beginTransaction().add(R.id.content_container, mStrategyFragment).commit();
        currentFragment = mStrategyFragment;

        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                switch (id) {
                    case R.id.menu_item_personal_center:
                        if (mPersonalFragment == null) {
                            mPersonalFragment = BaseFragment.newInstance(R.layout.fragment_personal_center);
                            mFragmentManager.beginTransaction().hide(currentFragment).add(R.id.content_container, mPersonalFragment).commit();
                        } else {
                            if (mPersonalFragment.isHidden())
                                mFragmentManager.beginTransaction().hide(currentFragment).show(mPersonalFragment).commit();
                        }
                        currentFragment = mPersonalFragment;
                        break;
                    case R.id.menu_item_qa:
                        if (mQAFragment == null) {
                            mQAFragment = BaseFragment.newInstance(R.layout.fragment_qa);
                            mFragmentManager.beginTransaction().hide(currentFragment).add(R.id.content_container, mQAFragment).commit();
                        } else {
                            if (mQAFragment.isHidden())
                                mFragmentManager.beginTransaction().hide(currentFragment).show(mQAFragment).commit();
                        }
                        currentFragment = mQAFragment;
                        break;
                    case R.id.menu_item_strategy:
                        if (mStrategyFragment == null) {
                            mStrategyFragment = BaseFragment.newInstance(R.layout.frament_strategy);
                            mFragmentManager.beginTransaction().hide(currentFragment).add(R.id.content_container, mStrategyFragment).commit();
                        }else {
                            if (mStrategyFragment.isHidden())
                                mFragmentManager.beginTransaction().hide(currentFragment).show(mStrategyFragment).commit();
                        }
                        currentFragment = mStrategyFragment;
                        break;
                    case R.id.menu_item_team:
                        if (mTeamFragment == null) {
                            mTeamFragment = BaseFragment.newInstance(R.layout.fragment_team);
                            mFragmentManager.beginTransaction().hide(currentFragment).add(R.id.content_container, mTeamFragment).commit();
                        }else {
                            if (mTeamFragment.isHidden())
                                mFragmentManager.beginTransaction().hide(currentFragment).show(mTeamFragment).commit();
                        }
                        currentFragment = mTeamFragment;
                }

                return true;
            }
        });
    }

    private void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }
}
